<?php
/**
 * image_manager.php
 * Image Manager Admin Interface
 * Version 1.8
 *
 * @author  Boudewijn Sjouke <boudewijnsjouke@hotmail.com>
 * @copyright Copyright 2009-2011
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0
 */
 
// Do some preliminary work
require('includes/application_top.php');
@ini_set('error_reporting', 1);
@ini_set('display_errors', '1');
@error_reporting(0);

// ===== Initialise =====
$image_array = array(); // Contains the filenames from the DB
$message = ""; // Message to display in the message block
if (!isset($_SESSION["actcnt"])) $_SESSION["actcnt"] = 1;
$prodmsg = ""; // Message to indicate that a products ID (filter) is not found

if (substr(strtoupper(PHP_OS), 0, 3) == "WIN") define('IM_IS_WIN', true);
else define('IM_IS_WIN', false);

// ===== Installation check to see if the SQL-patch has been executed =====
// The constant that is checked must not be in previous versions
if (defined('IM_BAK_DIR')) {

	// Get the input parameters or set them to default
	
	// Action
	$IMaction = (isset($_GET["IMaction"]) ? $_GET["IMaction"] : "");
	
	// Page
	$IMpage = (isset($_GET["IMpage"]) ? $_GET["IMpage"] : 1);
	
	// File parameter
	$image = (isset($_GET["image"]) ? urldecode($_GET["image"]) : "");
	
	// IMstatus: determine what images/products to see
	$IMstatus = (isset($_GET["IMstatus"]) ? $_GET["IMstatus"] : (IM_PRODUCT_STATUS_DEFAULT == "on" ? "Son" : (IM_PRODUCT_STATUS_DEFAULT == "off" ? "Soff" : "Sall")));
	
	// IMatt: determine what files to see: attached to a product or not
	$IMatt = (isset($_GET["IMatt"]) ? $_GET["IMatt"] : (IM_ATTACHED_DEFAULT == "not_attached" ? "Foff" : "Fon"));
	
	// Maybe it's overridden by the default action
	if (!isset($_GET['IMatt'])) {
		if (IM_STARTUP_ACTION == 'show_unattached') {
			$IMatt = "Foff";
			$IMmenuonly = false;
		}
		else if	(IM_STARTUP_ACTION == 'show_attached') {
			$IMatt = "Fon";
			$IMmenuonly = false;
		}
		else $IMmenuonly = true;
	}
	else $IMmenuonly = false;
	
	$IMmenuonly = (isset($_GET["IMatt"]) ? false : (IM_STARTUP_ACTION == "show_menu" ? true : false));
	
	// IMerror: see attached files with/without errors or all
	$IMerror = (isset($_GET["IMerror"]) ? $_GET["IMerror"] : (IM_SHOW_FILES_DEFAULT == "with_errors" ? "Eon" : (IM_SHOW_FILES_DEFAULT == "without_errors" ? "Eoff" : "Eall")));
	
	// IMimage: show either the image or a link
	$IMimage = (isset($_GET["IMimage"]) ? $_GET["IMimage"] : (IM_IMAGE_DEFAULT == "link" ? "Ioff" : (IM_IMAGE_DEFAULT == "small_images" ? "Ion" : "Iall")));
	
	// IMorder: order by....
	$IMorder = (isset($_GET["IMorder"]) ? $_GET["IMorder"] : (IM_SORT_ORDER_DEFAULT == "folder_filename" ? "Oimage" : (IM_SORT_ORDER_DEFAULT == "product_id" ? "OID" : "Olsize")));
	
	// IMfilter: find a specific products ID
	$IMfilter = (isset($_GET["IMfilter"]) ? $_GET["IMfilter"] : "");
	
	// If we search for a specific products id, we process all 
	if (!empty($IMfilter)) {
		$IMstatus = "Sall";
		$IMerror = "Eall";
	}
	// IMcont: set to 1 by the autocontinue function
	$IMcont = (isset($_GET["IMcont"]) ? true : false);

	$IMcanBAK = IMCheckBAKFolder(false);
	
	// ===== Perform the requested action. Either delete single file or perform resize or copy actions. =====
	if (!empty($IMaction)) {
		if ($IMaction == "deletefile") {
			if ($_GET["actcnt"] != $_SESSION["actcnt"]) {
				$filename = $_GET["filename"];
				$message .= (@unlink(DIR_FS_CATALOG_IMAGES . urldecode($filename)) ? '' : '<br />' . IM_CANNOT_DELETE . ' ' . urldecode($filename));
				$_SESSION["actcnt"] = $_GET["actcnt"];
			}
		}
		else $message = IMPerformAction($IMaction, $image);
	}
	
	// ===== Perform the multiple delete of selected files
	if (isset($_REQUEST['IMdeletefile'])) {
		foreach($_REQUEST['IMdeletefile'] as $key => $file) {
			$message .= (@unlink(DIR_FS_CATALOG_IMAGES . urldecode($file)) ? '' : '<br />' . IM_CANNOT_DELETE . ' ' . urldecode($file));
		}
	}
	
	// ===== Get both the arrays for filenames from the DB (from the products info) and files in the filesystem. =====
	$select_status = "";
	// If we selected attached files, we also get the product info from the DB (for sort/print)
	// Also we process the additional selector for status
	//
	// If we selected not attached files, we only get the filenames of the images for all products in the DB.
	if ($IMatt == "Fon" && !$IMmenuonly) {
		if ($IMstatus == "Son") $select_status = " AND p.products_status=1";
		else if ($IMstatus == "Soff") $select_status = " AND p.products_status=0";
		
		$products = $db->Execute("select (length(p.products_image) - length(replace(p.products_image, '/', ''))) as depth, p.products_id, pd.products_name, p.products_image, p.products_status, p.products_type, p.products_model from " . TABLE_PRODUCTS . " as p , ".TABLE_PRODUCTS_DESCRIPTION ." as pd where products_image != '' and p.products_id=pd.products_id".$select_status." and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'" . (empty($IMfilter) ? "" : " AND p.products_id='" . $IMfilter . "'") . " order by " . ($IMorder == "Oimage" ? "depth, p.products_image, ABS(p.products_id)" : "ABS(p.products_id)") . " ASC");

		while (!$products->EOF){
		  $image_array[$products->fields['products_image']][] = array($products->fields['products_id'], $products->fields['products_name'], $products->fields['products_status'], $products->fields['products_type'], $products->fields['products_model']);
		  $products->MoveNext();
		}
		if (count($image_array) == 0 && !empty($IMfilter)) $prodmsg = IM_NO_PROD_FOUND;
	}
	elseif (!$IMmenuonly) {
		$products = $db->Execute("select products_image from " . TABLE_PRODUCTS . " where products_image != ''");
		
		while (!$products->EOF){
		  $image_array[$products->fields['products_image']][] = array();
		  $products->MoveNext();
		}
	}
	// Get the filenames from the file folders. We need them in both cases
	$filelist = IMGetDirList(DIR_FS_CATALOG_IMAGES);
	$err_config = false;
}
else {
	$IMatt = "";
	$err_config = true;
}
	
// ===== Start output =====
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php print HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php print CHARSET; ?>">
<title><?php print TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<script language="javascript" type="text/javascript" src="includes/menu.js"></script>
<script language="javascript" type="text/javascript" src="includes/general.js"></script>
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" type="text/javascript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>

<script type="text/javascript">
	<!--
	function init() {
		cssjsmenu('navbar');
		if (document.getElementById) {
			var kill = document.getElementById('hoverJS');
			kill.disabled = true;
		}
	}

	function popupWindow(url) {
		window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=600,height=460,screenX=150,screenY=150,top=150,left=150')
	}
	
	function CheckForm(form) {
	    var cbResults = 0;
	    for (var i = 0; i < form.elements.length; i++ ) {
	        if (form.elements[i].type == 'checkbox' && form.elements[i].name == 'IMdeletefile[]') {
	            if (form.elements[i].checked == true) {
	                cbResults += 1;
	            }
	        }
	    }
	    if (cbResults > 0) return confirm('<?php print IM_CONFIRM_DELETE; ?>');
	    else return true;
	}
	
	function IMpopwin(url, name, w, h) {
		w += 32;
		h += 96;
		var win = window.open(url, name, 'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=no, resizable=no');
		 win.resizeTo(w, h);
		 win.focus();
	}
	
	function checkallboxes(state){
		for (c=0;c<=delboxes;c++) {
			if (document.getElementById('IMdelbox'+c) != null) {
				document.getElementById('IMdelbox'+c).checked=state;
			}
		}
	}

	// -->
</script>

</head>
<body onload="init()">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->

<div>
	<h1><?php print IM_HEADING_TITLE; ?></h1>
	<?php
	print IM_VERSION_TEXT . ":&nbsp;" . IM_VERSION_NUMBER;
	print "&nbsp;&nbsp;<a href=\"" . HTTP_SERVER . DIR_WS_ADMIN . IM_FILENAME_HELP . "\" target=\"_blank\" >" . IM_HELP . "</a><br /><br />";
	if ($err_config) print "<span class=\"errorText\">" . IM_NO_CONFIG . "</span>";
	?>
</div>

<div align="center">
<?php
print zen_draw_form('image_manager', IM_FILENAME, '', 'GET', 'onsubmit="return CheckForm(image_manager);"');

// Input for the page value, used in the page selector and page display
print '<input type="hidden" name="IMpage" value="' . $IMpage . '" />';

// ===== Print the block with all selectable options, including the submit button =====
if (!$err_config) IMPrintOptionsBlock($IMimage, $IMstatus, $IMatt, $IMerror, $IMorder, $IMfilter, $prodmsg, $IMcont);

?>
<br />
<?php
// ===== Print the message block =====
// Output of the function:
// Unattached: array of files that are not attached to a product
// Attached: array of attached files, with all needed links and info calculated and ready to print
if (!$err_config) $images = IMPrintMessageBlock($IMimage, $IMstatus, $IMatt, $IMerror, $IMorder, $IMpage, $IMorder, $IMfilter, $message);

// ===== Print the page for attached files =====
if ($IMatt == "Fon" && !$IMmenuonly) {
	// Only after the $images array is filled, we can sort the other array on large image size.
	if ($IMorder == "Olsize") {
		uksort($image_array, "IMSizeLSort");
	}
	// Only if we filter the results on errors or no errors, we walk through the array 
	if ($IMerror == "Eall") $new_image_array = $image_array;
	else {
		$new_image_array = array();
		foreach($image_array as $image => $product_array) {
			if (($IMerror == "Eon" && !$images[$image]["status_ok"]) || ($IMerror == "Eoff" && $images[$image]["status_ok"])) {
				$new_image_array[$image] = $product_array;
			}
		}
	}
	?>
	<br />
	<?php

	// Check if we have any combined actions. If so and more than 1, we can activate the autofix button. If more than 0, we can activate the function.
	if (IM_ENABLE_AUTOFIX == 'yes' && !$IMerrcheck && count($IMcombinedactions) > 0) { ?>
		<script type="text/javascript">
		<!--
		<?php
		if (count($IMcombinedactions) > 1 && !$IMcont) { ?>
			document.getElementById("IMautofixstart").style.display="inline";
		<?php }
		if (count($IMcombinedactions) > 0 && $IMcont) { ?>
			document.getElementById("IMautofixstop").style.display="inline";
		<?php 
		} 
		?>
		function IMstartcontinue() {
			pass = 'go';
			if(document.getElementById('IMautofixstopbutton') != null) {
				pass = document.getElementById('IMautofixstopbutton').value;
			}
			if (pass == 'go') {
				window.location.href="<?php print $IMcombinedactions[0]['url'];?>";
			}
			else {
				document.getElementById("IMautofixstop").style.display="none";
				<?php
				if (count($IMcombinedactions) > 1) { ?>
					document.getElementById("IMautofixstart").style.display="inline";
				<?php } ?>
				document.getElementById('IMautofixstopbutton').value = 'go';
			}
		}
		// -->
	</script>
	<?php
	}
	
	// Page selector
	$start = IMPrintPageSelector(count($new_image_array), $IMpage, 90); 
	$end = $start + IM_PAGE_LENGTH - 1;
	$count = 0;
	$colspan = 4;
	?>
	<table width=90%>
		<tr class="dataTableHeadingContent dataTableHeadingRow">
		<?php 
		// We don't have to print the first column if hovering is active instead of a link
			if (($IMimage == "Ioff" && IM_SHOW_POPUP_IMAGE == "link") || $IMimage != "Ioff") { ?>
			<td>
				<?php print IM_PICTURE_HEADER_SMALL; $colspan++; ?>
			</td>
			<?php }
			// columns for medium and large images, if all is selected
			if ($IMimage == "Iall") {
				// Only show medium size images if not disabled
				if (IM_USE_MEDIUM_IMAGES == "enable") { ?>
				<td>
					<?php print IM_PICTURE_HEADER_MEDIUM; $colspan++; ?>
				</td>
			<?php } ?>
			<td>
				<?php print IM_PICTURE_HEADER_LARGE; $colspan++; ?>
			</td>
			<?php } ?>
			<td>
				<?php print IM_FILENAME_HEADER; ?>
			</td>
			<td>
				<?php print IM_PRODUCT_HEADER; ?>
			</td>
			<td>
				<?php print IM_IMG_SMALL_HEADER; ?>
			</td>
			<?php 
			// Only show the medium data if medium images is not disabled
			if (IM_USE_MEDIUM_IMAGES == "enable") { ?>
			<td>
				<?php print IM_IMG_MEDIUM_HEADER; $colspan++; ?>
			</td>
			<?php } ?>
			<td>
				<?php print IM_IMG_LARGE_HEADER; ?>
			</td>
		</tr>
	<?php
	foreach($new_image_array as $image => $product_array) {
		if ($count >= $start) {
			if ($count > $end) break;
			?>
			<tr class="dataTableContent dataTableRow" style="vertical-align: top;">
				<?php if (($IMimage == "Ioff" && IM_SHOW_POPUP_IMAGE == "link") || $IMimage != "Ioff") { ?>
				<td align="center">
					<?php 
					// Print the image
					if ($images[$image]["small"]["exists"] && $images[$image]["small"]["is_image"]) {
						print $images[$image]["small"]["url"];
					}
					?>
				</td>
				<?php }
				if ($IMimage == "Iall") { 
					if (IM_USE_MEDIUM_IMAGES == "enable") { ?>
					<td align="center">
						<?php 
						// Print the medium image
						if ($images[$image]["medium"]["exists"] && $images[$image]["medium"]["is_image"]) {
							print $images[$image]["medium"]["url"];
						}
						?>
					</td>
					<?php } ?>
				<td align="center">
					<?php 
					// Print the large image
					if ($images[$image]["large"]["exists"] && $images[$image]["large"]["is_image"]) {
						print $images[$image]["large"]["url"];
					}
					?>
				</td>
				<?php } ?>
				<td>
					<?php if (isset($images[$image]["popuplink"])) {
						print "<script type=\"text/javascript\">\n<!--\n";
							print 'var img' . $count . '=\'<br />' . $images[$image]["popuplink"] . '\'';
							print "\n// -->\n</script>\n";
						print "<a href=\"#\" onmouseover=\"document.getElementById('imgpop" . $count . "').innerHTML=img" . $count . "; document.getElementById('imgpop" . $count . "').style.display='block';\" onmouseout=\"document.getElementById('imgpop" . $count . "').style.display='none';\">" . $image . "</a>";
						print '<div id="imgpop' . $count . '" style="display:none;"></div>';
					}
					else print $image;
					?>
				</td>
				<td>
					<?php 
					$first = true;
					foreach($product_array as $key => $product) {
						if (!$first) print "<br />";
						$first = false;
						if ($product[2] == 0) $span = " class=\"errorText\"";
						else $span = "";
						print  "<a".$span." href=\"" . zen_href_link(FILENAME_CATEGORIES, "cPath=" . zen_get_product_path($product[0], '0') . "&pID=" . $product[0] . "&action=new_product&product_type=".$product[3] . " \">" . $product[1]) . " (".$product[0].")</a>";
						print "<br />" . IM_TEXT_PRODUCT_MODEL . $product[4];
					}
					?>
				</td>
				<td>
					<?php 
					if ($images[$image]["small"]["exists"]) {
						if ($images[$image]["small"]["is_image"]) {
							// Print status
							print IM_IMG_STATUS.": ".IM_IMG_PRESENT."<br />";
							// Print size
							print IM_IMG_SIZE.": ";
							printf("%.0f Kb", $images[$image]["small"]["size"]);
							// Print measurements
							print "<br /><span";
							if ($images[$image]["small"]["too_big"]) print " class=\"errorText\"";
							print ">".IM_IMG_MEASURES.": ";
							print $images[$image]["small"]["width"]." x ".$images[$image]["small"]["height"]."</span>";
						}
						else print "<span class=\"errorText\">".IM_IMG_NOT_IMAGE."</span>";
					} 
					else print "<span class=\"errorText\">".IM_IMG_NOT_PRESENT."</span>"; 
					foreach ($images[$image]["action"]["small"] as $key => $action) {
						print "<br />";
						print $action;
					}
					?>
				</td>
				<?php if (IM_USE_MEDIUM_IMAGES == "enable") { ?>
					<td>
						<?php
						if ($images[$image]["medium"]["exists"]) {
							if ($images[$image]["medium"]["is_image"]) {
								// Print status
								print IM_IMG_STATUS.": ".IM_IMG_PRESENT."<br />";
								// Print size
								print IM_IMG_SIZE.": ";
								printf("%.0f Kb", $images[$image]["medium"]["size"]);
								// Print measurements
								print "<br /><span";
								if ($images[$image]["medium"]["too_big"]) print " class=\"errorText\"";
								print ">".IM_IMG_MEASURES.": ";
								print $images[$image]["medium"]["width"]." x ".$images[$image]["medium"]["height"]."</span>";
								print $images[$image]["medium"]["delete_link"];
							}
							else print "<span class=\"errorText\">".IM_IMG_NOT_IMAGE."</span>";
						} 
						else print "<span class=\"errorText\">".IM_IMG_NOT_PRESENT."</span>";
						foreach ($images[$image]["action"]["medium"] as $key => $action) {
							print "<br />";
							print $action;
						}
						?>
					</td>
				<?php } ?>
				<td>
					<?php
					if ($images[$image]["large"]["exists"]) {
						if ($images[$image]["large"]["is_image"]) {
							// Print status
							print IM_IMG_STATUS.": ".IM_IMG_PRESENT."<br />";
							// Print size
							print IM_IMG_SIZE.": ";
							printf("%.0f Kb", $images[$image]["large"]["size"]);
							// Print measurements
							print "<br /><span";
							if ($images[$image]["large"]["too_small"] || $images[$image]["large"]["too_big"]) print " class=\"errorText\"";
							print ">".IM_IMG_MEASURES.": ";
							print $images[$image]["large"]["width"]." x ".$images[$image]["large"]["height"]."</span>";
							print $images[$image]["large"]["delete_link"];
						}
						else print "<span class=\"errorText\">".IM_IMG_NOT_IMAGE."</span>";
					} 
					else print "<span class=\"errorText\">".IM_IMG_NOT_PRESENT."</span>"; 
					foreach ($images[$image]["action"]["large"] as $key => $action) {
						print "<br />";
						print $action;
					}
					?>
				</td>
			</tr>
		<?php 
		}
		$count++;
	}
	// If the product filter is set, we show attached images, that exist, and the upload folder is ok.......
	if (IM_SHOW_ADD_IMAGE_LINK == "yes" && !empty($IMfilter) && $IMatt == "Fon" && empty($prodmsg) && IMCheckUploadFolder(false)) {
		$mainimage = zen_get_products_image($IMfilter);
		$p = pathinfo($mainimage);
		if (IMDirIsWritable(DIR_FS_CATALOG_IMAGES . $p["dirname"] . '/')) {
			// Only show the link if the target directory is R/W
			?>
			<tr class="dataTableHeadingContent dataTableHeadingRow">
			<td colspan=<?php print $colspan; ?>>
			<?php print "<a href=\"javascript:" . IM_UPLOAD_ADDIMAGE . "\" onclick='popupWindow(\"" . zen_href_link(IM_UPLOAD_POPUP, 'imtype=addim&imname=' . urlencode($mainimage)) . "\"); return false;'>"  . IM_UPLOAD_ADDIMAGE . '</a></td></tr>';
		}
	}
	?>
	</table>
	<?php
	IMPrintPageSelector(count($new_image_array), $IMpage, 90);
	
	if (!$IMerrcheck && $IMcont && IM_ENABLE_AUTOFIX == 'yes' && count($IMcombinedactions) > 0) { ?>
		<script type="text/javascript">
		<!--
		(function (fn) {
			if (window.addEventListener) window.addEventListener('load', fn, false);
			else window.attachEvent('onload', fn);
		})
		(function() {
			setTimeout("IMstartcontinue()", <?php print (IM_CONTINUE_AUTOFIX * 1000); ?>);
		});
		// -->
	</script>
	<?php
	}
}
elseif ($IMatt == "Foff" && !$IMmenuonly) {
	// first we sort the file array, therefore we must convert it to a split path and filename array
	$files = array();
	foreach($images as $file => $properties) {
		$p = pathinfo($file);
		$files[] = array("fullfile" => $file, "dir" => $p["dirname"], "file" => $p["basename"], "ext" => $p["extension"], "relative_file" => str_replace(DIR_FS_CATALOG_IMAGES, "", $file), "canwrite" => $properties["canwrite"], "mismatch" => (isset($properties["mismatch"]) ? $properties["mismatch"] : false));
	}
	usort($files, "IMfilesort");	
	?>
	<br />
	<?php 
	$start = IMPrintPageSelector(count($files), $IMpage, 90); 
	$end = $start + IM_PAGE_LENGTH - 1;
	$checkboxcount = 0;
	?>	

	<div><table width=90%>
		<tr class="dataTableHeadingContent dataTableHeadingRow">
		<?php if (!($IMimage == "Ioff" && IM_SHOW_POPUP_IMAGE == "image")) { ?>
			<td align="center">
				<?php print IM_PICTURE_HEADER; ?>
			</td>
		<?php } ?>
			<td align="center">
				<?php print IM_SEL_DELETE; ?>
				<br /><span id='checklinks'>
				<a href="#" onclick='checkallboxes(true);return false;'><?php print IM_CHECK_ALL; ?></a>
				&nbsp;/&nbsp;
				<a href="#" onclick='checkallboxes(false);return false;'><?php print IM_UNCHECK_ALL; ?></a>
				</span>
			</td>
			<td>
				<?php print IM_FILENAME_HEADER; ?>
			</td>
			<td>
				<?php print IM_ACTION; ?>
			</td>
		</tr>
		<?php
		foreach($files as $key => $file) {
			if ($key >= $start) {
				if ($key > $end) break;
			?>	<tr class="dataTableContent dataTableRow" style="vertical-align: top;">
				<?php
				// Also filter out bak files, they will act as pictures but cannot display them
				if (strtolower($file["ext"]) != "txt" && $imgsize = @getimagesize($file["fullfile"])) {
					// Check if it's bigger than will be displayed 
					$cw = $imgsize[0] / SMALL_IMAGE_WIDTH;
					$ch = $imgsize[1] / SMALL_IMAGE_HEIGHT;
					
					// Prepare the link to show the image
					$fileurl = DIR_WS_CATALOG_IMAGES . $file["relative_file"];
					if ($IMimage == "Ion" || $IMimage == "Iall" || IM_SHOW_POPUP_IMAGE == "image") {
						if ($cw > $ch) $wh = "width=\"".SMALL_IMAGE_WIDTH."\"";
						else $wh = "height=\"".SMALL_IMAGE_HEIGHT."\"";
						$filelink = "<img src=\"" . $fileurl . "\" alt=\"" . $fileurl . "\" " . $wh." />";
					}
					else $filelink = "<a href=\"#\" onclick=\"IMpopwin('" . $fileurl . "', 'IMpopup', " . $imgsize[0] . ", " . $imgsize[1] . "); return false;\">" . IM_SELECT_IMG_LINK_CLICK ."</a>";
				}
				else $filelink = "";
				?>
				<?php if (!($IMimage == "Ioff" && IM_SHOW_POPUP_IMAGE == "image")) { ?>
					<td align="center">
					<?php print $filelink; ?>
					</td>
				<?php } ?>
					<td align="center">
						<?php 
							if ($file["canwrite"]) {
								print zen_draw_checkbox_field('IMdeletefile[]', urlencode($file["relative_file"]), false, '', 'id="IMdelbox'.$checkboxcount.'"');
								$checkboxcount++;
							}
							else print "&nbsp;";
						 ?>
					</td>
					<td>
						<?php 
						$name_mismatch = ($file["mismatch"] ? "<br /><span class='errorText'>" . IM_NAME_MISMATCH . "</span>" : "");
						if ($IMimage == "Ioff" && IM_SHOW_POPUP_IMAGE == "image") {
							if (!empty($filelink)) {
								print "<script type=\"text/javascript\">\n<!--\n";
 								print 'var img' . $key . '=\'<br />' . $filelink . '\'';
 								print "\n// -->\n</script>\n";
								print "<a href=\"#\" onmouseover=\"document.getElementById('imgpop" . $key . "').innerHTML=img" . $key . "; document.getElementById('imgpop" . $key . "').style.display='block';\" onmouseout=\"document.getElementById('imgpop" . $key . "').style.display='none';\">" . ($file["canwrite"] ? $file["fullfile"] : '<span class="errorText">' . $file["fullfile"] . '</span>') . "</a>" . $name_mismatch;
								print '<div id="imgpop' . $key . '" style="display:none;"></div>';
							}
							else print ($file["canwrite"] ? $file["fullfile"] : '<span class="errorText">' . $file["fullfile"] . '</span>') . $name_mismatch;
						}
						else print ($file["canwrite"] ? $file["fullfile"] : '<span class="errorText">' . $file["fullfile"] . '</span>') . $name_mismatch;
						?>
					</td>
					<td>
						<?php 
						if ($file["canwrite"]) print "<a href=\"" . zen_href_link(IM_FILENAME, "IMpage=" . $IMpage . "&IMimage=" . $IMimage . "&IMstatus=" . $IMstatus ."&IMatt=" . $IMatt . "&IMerror=" . $IMerror . "&IMorder=" . $IMorder . "&IMaction=deletefile&filename=" . urlencode($file["relative_file"])). "\" />". IM_IMAGE_DELETE . "</a>";
						else print "&nbsp;";
						?>
					</td>
				</tr>
			<?php }
		} ?>
		</table></div>
	<?php
	IMPrintPageSelector(count($files), $IMpage, 90); 
	?>
	<script type="text/javascript">
	<!--
		<?php
		if ($checkboxcount < 2) { ?>
		document.getElementById('checklinks').innerHTML='';
		<?php } ?>
		var delboxes = <?php print $checkboxcount; ?>;
		// -->
	</script>
<?php
}
?>
</form>		
</div>		

<!-- body_eof //-->
<!-- footer //-->
<?php
	require(DIR_WS_INCLUDES . 'footer.php');
?>
<!-- footer_eof //-->
</body>
</html>
<?php
	require(DIR_WS_INCLUDES . 'application_bottom.php');
?>